package service;

import dao.RegRecordDao;
import model.RegRecord;
import util.DbUtil;  // 修改为使用 DbUtil

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class RegRecordService {
    // 更新支付状态
    public static void updatePaymentStatus(Integer recordId, Date payTime) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            String sql = "UPDATE reg_record SET pay_time = ? WHERE id = ?";
            conn = DbUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setTimestamp(1, new Timestamp(payTime.getTime()));
            ps.setInt(2, recordId);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新支付状态失败", e);
        } finally {
            DbUtil.release(ps, conn);
        }
    }

    public static List<RegRecord> query(Integer patientId, Integer deptId, String beginDate, String endDate)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<RegRecord> records = new LinkedList<>();
        if(beginDate != null && endDate != null) {
            Date begin = sdf.parse(beginDate);
            Date end = sdf.parse(endDate);
            records = RegRecordDao.select(null, patientId, deptId, begin, end, null);
        } else {
            records = RegRecordDao.select(null, patientId, deptId, null, null, null);
        }
        return records;
    }
}